package com.learn.tcp;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * TCP网络编程：
 * 客户端发文件给服务端，服务端将文件保存，并返回信息给客户端
 */
public class ExchangeMassage {
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        FileInputStream fis = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);

            os = socket.getOutputStream();

            fis = new FileInputStream(new File("jerry.jpg"));

            //上传文件给服务端
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }

            //终止socket数据传输，告诉server传输完成
            socket.shutdownOutput();

            //接受服务端返回消息
            is = socket.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bufferReceive = new byte[1024];
            int lenReceive;
            if((lenReceive = is.read()) != -1){
                byteArrayOutputStream.write(bufferReceive, 0, lenReceive);
            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(os != null){
                    os.close();
                }
                if(socket != null){
                    socket.close();
                }
                if (fis != null){
                    fis.close();
                }
                if(is != null){
                    is.close();
                }
                if(byteArrayOutputStream != null){
                    byteArrayOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;

        ByteArrayOutputStream byteArrayOutputStream = null;

        try {
            serverSocket = new ServerSocket(9090);
            socket = serverSocket.accept();

            is = socket.getInputStream();
            fos = new FileOutputStream(new File("jerryjerry.jpg"));

            //接收服务文件，并写在本地
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer, 0, len);
            }

            //向服务端返回信息
            os = socket.getOutputStream();
            os.write("上传成功".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
